using System;
using Microsoft.AspNetCore.Mvc;
using GoodroommateComputingPlatform.Tasks;
using GoodroommateComputingPlatform.Tasks.Goodroommate;
using GoodroommateComputingPlatform.Models;
using System.Collections.Generic;

namespace GoodroommateComputingPlatform.Controllers
{
    [Route("api/tasks")]
    public class TaskApiController : Controller
    {
        ITaskManager _tm;

        public TaskApiController(ITaskManager tm, TaskLoader loader)
        {
            _tm = tm;
        }

        [HttpGet("status")]
        public ServerStatus GetStatus()
        {
            var status = new ServerStatus();
            status.Name = "Goodroommate Computing Platform";
            return status;
        }

        [HttpGet("list")]
        public TaskListWrapper GetAllTasks()
        {
            return new TaskListWrapper()
            {
                List = _tm.GetAllTasks()
            };
        }

        [HttpGet("status/{taskName}")]
        public IActionResult GetTaskStatus(string taskName)
        {
            try
            {
                return new ObjectResult(_tm.GetTaskStatus(taskName));
            }
            catch (ArgumentException)
            {
                return new NotFoundResult();
            }
        }

        [HttpPost("start/{taskName}")]
        public IActionResult StartTask(string taskName)
        {
            try
            {
                _tm.StartTaskIfIdle(taskName, null, null);
                return new ObjectResult(_tm.GetTaskStatus(taskName));
            }
            catch (ArgumentException)
            {
                return new NotFoundResult();
            }
        }

    }
}
